package com.ytjj.qmyx.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.AdminModuleLogDao;
import com.ytjj.qmyx.admin.dao.ReturnGoodsDao;
import com.ytjj.qmyx.admin.eunm.ModuleEnum;
import com.ytjj.qmyx.admin.mapper.AdminModuleLogMapper;
import com.ytjj.qmyx.admin.mapper.ReturnDutyMapper;
import com.ytjj.qmyx.admin.model.*;
import com.ytjj.qmyx.admin.model.response.AdminModuleLogResponse;
import com.ytjj.qmyx.admin.model.response.ReturnGoodsResponse;
import com.ytjj.qmyx.admin.model.vo.GoodReturnInfoVO;
import com.ytjj.qmyx.admin.model.vo.ReturnDutyVO;
import com.ytjj.qmyx.admin.service.AdminModuleLogService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import com.ytjj.qmyx.mall.mapper.ReturnGoodsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author jiawei
 * @date 2023年03月23日 13:58
 * @describe
 */
@Service
@Slf4j
public class AdminModuleLogServiceImpl implements AdminModuleLogService {

    @Autowired
    AdminModuleLogMapper adminModuleLogMapper;
    @Autowired
    ReturnDutyMapper returnDutyMapper;
    @Autowired
    AdminModuleLogDao adminModuleLogDao;
    @Autowired
    ReturnGoodsDao returnGoodsDao;

    @Override
    public Integer recordReturnDuty(Object o,String type,Integer optType,String relevantNo,String parentRelevantNo) {
        int insert = 0;
        try {
            Admin admin = UserUtil.getAdmin();
            AdminModuleLog adminModuleLog = new AdminModuleLog();
            ReturnDutyVO emptyDutyVO = new ReturnDutyVO();
            BeanUtils.copyProperties(o,emptyDutyVO);
            adminModuleLog.setAdminId(admin.getId());
            adminModuleLog.setAdminName(admin.getName());
            adminModuleLog.setModuleName(type);
            adminModuleLog.setOptType(optType);
            adminModuleLog.setRelevantNo(relevantNo);
            //父关联号
            adminModuleLog.setParentRelevantNo(parentRelevantNo);
            AdminModuleLog entity = new AdminModuleLogResponse();
            entity.setModuleName(ModuleEnum.RETURN_DUTY.getType());
            entity.setRelevantNo(relevantNo);
            AdminModuleLog latestLogByEntity = adminModuleLogDao.getLatestLogByEntity(entity);
            //新增
            if(latestLogByEntity == null){
                adminModuleLog.setContentJson(JSON.toJSONString(emptyDutyVO));
            }else {//修改
                String contentJson = latestLogByEntity.getContentJson();
                ReturnDutyVO returnDutyVO = JSON.parseObject(contentJson, ReturnDutyVO.class);
                BeanUtil.copyProperties(emptyDutyVO,returnDutyVO, CopyOptions.create().setIgnoreNullValue(true));
                adminModuleLog.setContentJson(JSON.toJSONString(returnDutyVO));
            }
            insert = adminModuleLogMapper.insertSelective(adminModuleLog);
        } catch (BeansException e) {
            log.error("记录客服同步到退货管理出错！,{}",e);
        }
        return insert;
    }

    @Override
    public Integer recordReturnBankerInfo(GoodReturnInfoVO goodReturnInfoVO, String type, Integer optType, String relevantNo) {
        Admin admin = UserUtil.getAdmin();
        AdminModuleLog adminModuleLog = new AdminModuleLog();
        adminModuleLog.setAdminId(admin.getId());
        adminModuleLog.setAdminName(admin.getName());
        adminModuleLog.setModuleName(type);
        adminModuleLog.setOptType(optType);
        adminModuleLog.setRelevantNo(relevantNo);
        adminModuleLog.setContentJson(JSON.toJSONString(goodReturnInfoVO));
        //父关联号
        ReturnGoodsResponse returnGoodsResponse = returnGoodsDao.selectByServiceNo(relevantNo);
        adminModuleLog.setParentRelevantNo(returnGoodsResponse.getChildOrderNo());
        int insert = adminModuleLogMapper.insertSelective(adminModuleLog);
        return insert;
    }

    @Override
    public List<AdminModuleLogResponse> getAdminModuleLogs(AdminModuleLog moduleName) {
        List<AdminModuleLog> adminModuleLogs = adminModuleLogDao.getAdminModuleLogs(moduleName);
        List<AdminModuleLogResponse> responses = new ArrayList<>();
        adminModuleLogs.stream().forEach(e->{
            AdminModuleLogResponse adminModuleLogResponse = new AdminModuleLogResponse();
            BeanUtils.copyProperties(e,adminModuleLogResponse);
            Object obj = JSON.parseObject(e.getContentJson());
            adminModuleLogResponse.setContentJson(null);
            adminModuleLogResponse.setContentObj(obj);
            responses.add(adminModuleLogResponse);
        });
        return responses;
    }
}
